<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="买家名称" prop="buyerName">
        <el-input
          v-model="queryParams.buyerName"
          placeholder="请输入买家名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票信息" prop="invoiceInfo">
        <el-input
          v-model="queryParams.invoiceInfo"
          placeholder="请输入发票信息"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 订单状态筛选 -->
    <div class="order-status-filter">
      <div class="filter-item" 
           :class="{ active: activeStatus === '' }" 
           @click="handleStatusFilter('')">
        全部
      </div>
      <div class="filter-item" 
           :class="{ active: activeStatus === '未付款' }" 
           @click="handleStatusFilter('未付款')">
        未付款
      </div>
      <div class="filter-item" 
           :class="{ active: activeStatus === '已付款' }" 
           @click="handleStatusFilter('已付款')">
        已付款
      </div>
      <div class="filter-item" 
           :class="{ active: activeStatus === '待发货' }" 
           @click="handleStatusFilter('待发货')">
        待发货
      </div>
      <div class="filter-item" 
           :class="{ active: activeStatus === '部分发货' }" 
           @click="handleStatusFilter('部分发货')">
        部分发货
      </div>
      <div class="filter-item" 
           :class="{ active: activeStatus === '已发货' }" 
           @click="handleStatusFilter('已发货')">
        已发货
      </div>
      <div class="filter-item" 
           :class="{ active: activeStatus === '待核验' }" 
           @click="handleStatusFilter('待核验')">
        待核验
      </div>
      <div class="filter-item" 
           :class="{ active: activeStatus === '待自提' }" 
           @click="handleStatusFilter('待自提')">
        待自提
      </div>
      <div class="filter-item" 
           :class="{ active: activeStatus === '已完成' }" 
           @click="handleStatusFilter('已完成')">
        已完成
      </div>
      <div class="filter-item" 
           :class="{ active: activeStatus === '已关闭' }" 
           @click="handleStatusFilter('已关闭')">
        已关闭
      </div>
    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['orders:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['orders:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['orders:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['orders:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="买家名称" align="center" prop="buyerName" />
      <el-table-column label="订单来源" align="center" prop="orderSource" />
      <el-table-column label="订单类型" align="center" prop="orderType" />
      <el-table-column label="订单金额" align="center" prop="orderAmount" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" />
      <el-table-column label="下单时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发票信息" align="center" prop="invoiceInfo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['orders:order:edit']">修改</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['orders:order:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联用户表的用户ID" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入关联用户表的用户ID" />
        </el-form-item>
        <el-form-item label="订单来源" prop="orderSource">
          <el-input v-model="form.orderSource" placeholder="请输入订单来源" />
        </el-form-item>
        <el-form-item label="订单金额" prop="orderAmount">
          <el-input v-model="form.orderAmount" placeholder="请输入订单金额" />
        </el-form-item>
        <el-form-item label="收货人姓名" prop="consigneeName">
          <el-input v-model="form.consigneeName" placeholder="请输入收货人姓名" />
        </el-form-item>
        <el-form-item label="收货人电话" prop="consigneePhone">
          <el-input v-model="form.consigneePhone" placeholder="请输入收货人电话" />
        </el-form-item>
        <el-form-item label="收货地址" prop="consigneeAddress">
          <el-input v-model="form.consigneeAddress" placeholder="请输入收货地址" />
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-input v-model="form.paymentMethod" placeholder="请输入支付方式" />
        </el-form-item>
        <el-form-item label="买家留言" prop="buyerNote">
          <el-input v-model="form.buyerNote" placeholder="请输入买家留言" />
        </el-form-item>
        <el-form-item label="发票信息" prop="invoiceInfo">
          <el-input v-model="form.invoiceInfo" placeholder="请输入发票信息" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/orders/order"

const { proxy } = getCurrentInstance()

const orderList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const activeStatus = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    buyerName: null,
    orderType: null,
    invoiceInfo: null,
    orderStatus: null  // 后端期望的参数名是 orderStatus
  },
  rules: {
    memberId: [
      { required: true, message: "关联用户表的用户ID不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询商品订单列表 */
function getList() {
  loading.value = true
  listOrder(queryParams.value).then(response => {
    orderList.value = response.rows
    // 调试信息
    console.log('查询到的订单列表:', response.rows)
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    memberId: null,
    buyerName: null,
    buyerPhone: null,
    orderSource: null,
    orderType: null,
    orderAmount: null,
    orderStatus: null,
    createTime: null,
    consigneeName: null,
    consigneePhone: null,
    consigneeAddress: null,
    paymentMethod: null,
    buyerNote: null,
    deliveryMethod: null,
    invoiceInfo: null
  }
  proxy.resetForm("orderRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  activeStatus.value = ""
  queryParams.value.orderStatus = null  // 修改为正确的参数名
  handleQuery()
}

// 订单状态筛选
function handleStatusFilter(status) {
  activeStatus.value = status
  queryParams.value.orderStatus = status === '' ? null : status  // 修改为正确的参数名
  queryParams.value.pageNum = 1
  
  // 调试信息
  console.log('选中的状态:', status)
  console.log('传递给后端的参数:', queryParams.value)
  
  getList()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加商品订单"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _orderId = row.orderId || ids.value
  getOrder(_orderId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改商品订单"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate(valid => {
    if (valid) {
      if (form.value.orderId != null) {
        updateOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orderIds = row.orderId || ids.value
  proxy.$modal.confirm('是否确认删除商品订单编号为"' + _orderIds + '"的数据项？').then(function() {
    return delOrder(_orderIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('orders/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}

/** 查看按钮操作 */
function handleView(row) {
  const orderId = row.orderId
  proxy.$router.push(`/orders/order/detail/${orderId}`)
}

getList()
</script>

<style scoped>
.order-status-filter {
  background-color: #f5f5f5;
  padding: 12px 20px;
  margin-bottom: 16px;
  border-radius: 4px;
  display: flex;
  gap: 20px;
  align-items: center;
  border-top: 2px solid #ff6600;
}

.filter-item {
  padding: 6px 16px;
  border-radius: 16px;
  cursor: pointer;
  font-size: 14px;
  color: #666;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.filter-item:hover {
  background-color: #e0e0e0;
  color: #333;
}

.filter-item.active {
  background-color: #fff;
  color: #ff6600;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  font-weight: 500;
}
</style>
